<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>带日志的弹窗示例</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      padding: 20px;
      background-color: #f4f4f4;
    }

    button {
      padding: 10px 15px;
      margin: 5px;
      font-size: 16px;
      cursor: pointer;
      background-color: #007bff;
      color: white;
      border: none;
      border-radius: 4px;
    }

    button:hover {
      background-color: #0056b3;
    }

    /* 日志框样式 */
    #log {
      margin-top: 20px;
      padding: 10px;
      border: 1px solid #ccc;
      background-color: #fff;
      border-radius: 4px;
      min-height: 50px;
      max-height: 200px;
      overflow-y: auto;
      font-size: 14px;
      color: #333;
    }

    .log-entry {
      padding: 4px 0;
      border-bottom: 1px dashed #eee;
    }

    .log-entry.success {
      color: green;
    }

    .log-entry.error {
      color: red;
    }

    /* 自定义模态框样式 */
    .modal-overlay {
      display: none;
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background-color: rgba(0, 0, 0, 0.5);
      justify-content: center;
      align-items: center;
      z-index: 1000;
    }

    .modal {
      background: white;
      padding: 20px;
      width: 300px;
      border-radius: 8px;
      text-align: center;
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    }

    .modal button {
      margin: 10px 5px;
    }
  </style>
</head>
<body>

  <h1>弹窗示例：带网页日志输出</h1>

  <!-- 操作按钮 -->
  <button onclick="showAlert()">原生 Alert</button>
  <button onclick="showConfirm()">原生 Confirm</button>
  <button onclick="showCustomModal()">自定义弹窗（带回调）</button>

  <!-- 自定义模态框 -->
  <div id="customModal" class="modal-overlay">
    <div class="modal">
      <h3>这是自定义弹窗</h3>
      <p>请选择操作：</p>
      <button onclick="handleCustomConfirm()">确定</button>
      <button onclick="handleCustomCancel()">取消</button>
    </div>
  </div>

  <!-- 日志显示框 -->
  <h3>操作日志</h3>
  <div id="log"></div>

  <script>
    // 获取日志元素
    const logElement = document.getElementById("log");

    // 添加日志的函数
    function log(message, type = "") {
      const entry = document.createElement("div");
      entry.className = `log-entry ${type}`;
      entry.textContent = `[${new Date().toLocaleTimeString()}] ${message}`;
      logElement.appendChild(entry);
      logElement.scrollTop = logElement.scrollHeight; // 滚动到底部
    }

    // 1. 原生 alert
    function showAlert() {
      alert("这是一个原生 Alert 提示框！");
      log("Alert 显示：用户点击了确定按钮", "success");
    }

    // 2. 原生 confirm
    function showConfirm() {
      const result = confirm("你确定要执行此操作吗？");
      if (result) {
        log("Confirm：用户点击了【确定】", "success");
        onConfirmCallback();
      } else {
        log("Confirm：用户点击了【取消】", "error");
        onCancelCallback();
      }
    }

    // confirm 的回调处理（不弹窗，只写日志）
    function onConfirmCallback() {
      log("回调执行：确认操作已完成", "success");
    }

    function onCancelCallback() {
      log("回调执行：取消操作已处理", "error");
    }

    // 3. 自定义弹窗
    const modal = document.getElementById("customModal");

    function showCustomModal() {
      log("自定义弹窗：已打开");
      modal.style.display = "flex";
    }

    function closeCustomModal() {
      modal.style.display = "none";
    }

    // 自定义弹窗 - 确定回调
    function handleCustomConfirm() {
      log("自定义弹窗：用户点击了【确定】", "success");
      closeCustomModal();
      customConfirmCallback();
    }

    // 自定义弹窗 - 取消回调
    function handleCustomCancel() {
      log("自定义弹窗：用户点击了【取消】", "error");
      closeCustomModal();
      customCancelCallback();
    }

    // 自定义回调函数（只写日志，不弹窗）
    function customConfirmCallback() {
      log("自定义回调执行：确认逻辑已完成", "success");
    }

    function customCancelCallback() {
      log("自定义回调执行：取消逻辑已完成", "error");
    }
  </script>

</body>
</html>